// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "name": {
    "rules": [
      {
        "format": "string",
        "errorMessage": "姓名不能为空"
      }
    ],
    "title": "医生姓名",
    "label": "医生姓名"
  },
  "picture": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "title": "医生图片",
    "label": "医生图片"
  },
  "job": {
    "rules": [
      {
        "format": "string",
        "errorMessage": "职称不能为空"
      }
    ],
    "title": "职称",
    "label": "职称"
  },
  "department": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "text": "外科",
            "value": "外科"
          },
          {
            "text": "内科",
            "value": "内科"
          },
          {
            "text": "妇科",
            "value": "妇科"
          },
          {
            "text": "儿童科",
            "value": "儿童科"
          }
        ]
      }
    ],
    "title": "所属科室",
    "label": "所属科室"
  },
  "main": {
    "rules": [
      {
        "format": "string",
        "errorMessage": "主治不能为空"
      }
    ],
    "title": "主治",
    "label": "主治"
  },
  "money": {
    "rules": [
      {
        "format": "int",
        "errorMessage": "挂号费不能为空，且为整数"
      }
    ],
    "title": "挂号费",
    "label": "挂号费"
  }
}

const enumConverter = {
  "department_valuetotext": {
    "外科": "外科",
    "内科": "内科",
    "妇科": "妇科",
    "儿童科": "儿童科"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
